package com.serviets;

import com.dao.BookDao;
import com.dao.impl.BookDaoimpl;
import com.model.Book;
import com.model.BookView;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;


/**
 * 查询数据的控制层，调用数据访问层获取数据
 */
@WebServlet("/book.do")
public class BookServiets extends BaseServiets {

    private BookDao bookDao = new BookDaoimpl();

    protected void queryEmps(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ServletException {
        List<BookView> bookViews = bookDao.queryEmps(1);
        request.setAttribute("bookList", bookViews);
        request.getRequestDispatcher("showemp.jsp").forward(request, response);
    }

    protected void addBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String bookName = request.getParameter("bookName");
        String bookPrice = request.getParameter("bookPrice");
        String bookUpload_time = request.getParameter("bookUpload_time");
        String bookBorrow_id = request.getParameter("bookBorrow_id");
        String bookBorrow_time = request.getParameter("bookBorrow_time");
        int bookState = Integer.parseInt(request.getParameter("bookState"));
        Book book = new Book();
        book.setB_name(bookName);
        book.setB_price(bookPrice);
        book.setUpload_time(bookUpload_time);
        book.setBorrow_id(bookBorrow_id);
        book.setBorrow_time(bookBorrow_time);
        book.setState(bookState);
        bookDao.addBook(book);
        System.out.println("book = " + book);
        request.getRequestDispatcher("book.do?operate=queryEmps").forward(request, response);
    }
    protected void updateEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获得客户端提交的数据
        int bookId = Integer.parseInt(request.getParameter("bookId"));
        String bookName = request.getParameter("bookName");
        String bookPrice = request.getParameter("bookPrice");
        String bookUpload_time = request.getParameter("bookUpload_time");
        String bookBorrow_id = request.getParameter("bookBorrow_id");
        String bookBorrow_time = request.getParameter("bookBorrow_time");
        int bookState = Integer.parseInt(request.getParameter("bookState"));

        //创建emp的对象
        Book book = new Book();
        book.setId(bookId);
        book.setB_name(bookName);
        book.setB_price(bookPrice);
        book.setUpload_time(bookUpload_time);
        book.setBorrow_id(bookBorrow_id);
        book.setBorrow_time(bookBorrow_time);
        book.setState(bookState);
        //调用方法完成添加功能
        bookDao.updateEmp(book);
        //跳转页面
        request.getRequestDispatcher("emp.do?operate=queryEmps").forward(request, response);

    }

}


